home *** CD-ROM | disk | FTP | other *** search
- /*
- listing 6 - search.c (sample program)
- */
-
- #include <stdio.h>
- #include "common.h" /* shared by all files */
-
- /*
- Initialization of the tran_record file. This
- would normally be done through file input.
- */
-
- struct record tran_record[] = {
- 0, 0, 0.0, "zero",
- 1, 11, 11.1, "one",
- 2, 22, 22.2, "two",
- 3, 33, 33.3, "three",
- 4, 44, 44.4, "four",
- };
-
- struct field_definition definitions[] = {
- "id", INT, &tran_record[0].id,
- "number", INT, &tran_record[0].number,
- "price", FLOAT, &tran_record[0].price,
- "code", STRING, &tran_record[0].code,
- "last", NULL, NULL,
- };
-
- main()
- {
-
- /* loop control */
-
- int i;
-
- /* holds definition type */
-
- char user_keyword[10];
-
- /* ascii string search key */
-
- char user_value[100];
-
- /* transaction_size of the transaction array */
-
- int transaction_size;
-
-
- /* calculate size of a transaction */
-
- transaction_size =
- sizeof(tran_record)/sizeof(tran_record[0]);
-
- /* set up the test to search for integer id */
-
- strcpy (user_value, "0");
- strcpy (user_keyword, "id");
- printf ("\nuser_value = %s\n", user_value);
- printf ("user_keyword = %s\n", user_keyword);
- if ( (type_check(definitions, tran_record,
- transaction_size,
- user_keyword, user_value) != SUCCEED) )
- printf ("Error: search failed\n");
-
- /* set up the test to search for integer number */
-
- strcpy (user_value, "11");
- strcpy (user_keyword, "number");
- printf ("\nuser_value = %s\n", user_value);
- printf ("user_keyword = %s\n", user_keyword);
- if ( (type_check(definitions, tran_record,
- transaction_size,
- user_keyword, user_value) != SUCCEED) )
- printf ("Error: search failed\n");
-
- /* set up the test to search for float */
-
- strcpy (user_value, "33.3");
- strcpy (user_keyword, "price");
- printf ("\nuser_value = %s\n", user_value);
- printf ("user_keyword = %s\n", user_keyword);
- if ( (type_check(definitions, tran_record,
- transaction_size,
- user_keyword, user_value) != SUCCEED) )
- printf ("Error: search failed\n");
-
- /* set up the test to search for string */
-
- strcpy (user_value,"two");
- strcpy (user_keyword, "code");
- printf ("\nuser_value = %s\n", user_value);
- printf ("user_keyword = %s\n", user_keyword);
- if ( (type_check(definitions, tran_record,
- transaction_size,
- user_keyword, user_value) != SUCCEED) )
- printf ("Error: search failed\n");
-
- /* set up the test to return 'No match' */
-
- strcpy (user_value, "99");
- strcpy (user_keyword, "number");
- printf ("\nuser_value = %s\n", user_value);
- printf ("user_keyword = %s\n", user_keyword);
- if ( (type_check(definitions, tran_record,
- transaction_size,
- user_keyword, user_value) != SUCCEED) )
- printf ("Error: search failed\n");
-
- /*
- Generate invalid search,
- CHAR is not define as a keyword
- */
-
- strcpy (user_value, "99");
- strcpy (user_keyword, "char");
- printf ("\nuser_value = %s\n", user_value);
- printf ("user_keyword = %s\n", user_keyword);
- if ( (type_check(definitions, tran_record,
- transaction_size,
- user_keyword, user_value) != SUCCEED) )
- printf ("Error: search failed\n");
-
- exit(0);
-
- }
-